package com.feibi.study.UI

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.viewpager.widget.PagerAdapter
import com.bumptech.glide.Glide
import com.feibi.study.Basic.BasicActivity
import com.feibi.study.R
import kotlinx.android.synthetic.main.activity_view_pager.*

class ViewPagerActivity : BasicActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_view_pager)
        view_pager.adapter = WelcomePagerAdapter(this)

    }

    class WelcomePagerAdapter(var activity: BasicActivity) : PagerAdapter() {
        var resourceIds = arrayOf(R.mipmap.welcome1, R.mipmap.welcome2, R.mipmap.welcome3)

        override fun getCount(): Int {
            return 3
        }

        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return view === `object`
        }

        @SuppressLint("CheckResult")
        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val imageView = ImageView(activity)
            Glide.with(activity).load(resourceIds[position]).into(imageView)
            imageView.setOnClickListener {
                if (position == 2) {
                    activity.finish()
                }
            }
            container.addView(imageView)
            return imageView
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeView(`object` as View)
        }

        override fun getItemPosition(`object`: Any): Int {
            return POSITION_NONE
        }

    }

}